package com.it.travel.controller;

import com.it.travel.pojo.User;
import com.it.travel.service.UserService;
import com.it.travel.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册账户
     * @param user
     * @param check
     * @param session
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public ResultInfo userRegister(User user, @RequestParam("check") String check, HttpSession session) {
        String check_coder = (String) session.getAttribute("CHECK_CODER");
        return userService.register(user,check,check_coder);

    }

    /**
     * 激活账户
     * @param code
     * @return
     */
    @RequestMapping("/activate")
    public String getActivate(@RequestParam("code") String code) {
        Boolean is =userService.activateUser(code);
        if (!is) {
            throw new RuntimeException("激活失败");
        }
        return "redirect:/login.html";
    }


    /**
     * 账户登录
     * @param user
     * @param check
     * @param session
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public ResultInfo loginUser(User user,@RequestParam("check") String check,HttpSession session) {
        String check_coder = (String) session.getAttribute("CHECK_CODER");
        return userService.loginUser(user,check,check_coder);
    }


    /**
     * 登录信息表头username数据回显
     * @return
     */
    @RequestMapping("getLoginUserData")
    @ResponseBody
    public ResultInfo getSessionData() {
        return userService.getData();
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping("loginOut")
    public String loginOut() {
        Boolean is=userService.loginOut();
        if (!is) {
            throw new RuntimeException("退出失败");
        }
        return "redirect:/index.html";
    }
}
